//
//  MessageTableViewCell.m
//  BGM
//
//  Created by liuqin on 2018/11/26.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "MessageTableViewCell.h"
#import "Common.h"
#import "UIImageView+WebCache.h"
@interface MessageTableViewCell()

@property (nonatomic, strong)UIImageView *headImage;
@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)UILabel *messageLabel;
@property (nonatomic, strong)UILabel *timeLabel;

@property (nonatomic, strong)UILabel *line;



@end


@implementation MessageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
        self.headImage.layer.cornerRadius = 25;
        [self.headImage setImage:[UIImage imageNamed:@"headImage"]];
        self.headImage.layer.masksToBounds = YES;
        [self addSubview:self.headImage];
        
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, SCREENW-150, 30)];
        self.nameLabel.font = [UIFont systemFontOfSize:16];
        self.nameLabel.textColor = bgmmainColor;
        [self addSubview:self.nameLabel];
        
        self.messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 35, SCREENW-100, 30)];
        self.messageLabel.font = [UIFont systemFontOfSize:13];
        self.messageLabel.textColor = bgmgrayColor;
        [self addSubview:self.messageLabel];
        
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENW-140, 5, 130, 30)];
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.textColor = bgmgrayColor;
        [self addSubview:self.timeLabel];
        
        
        self.readStatus = [[UILabel alloc]initWithFrame:CGRectMake(SCREENW-30, 45, 10, 10)];
        self.readStatus.backgroundColor = bgmyelloeColor;
        self.readStatus.layer.cornerRadius = 5;
        self.readStatus.layer.masksToBounds = YES;
        [self addSubview:self.readStatus];
        self.readStatus.hidden = YES;
        
        
        self.helpImage = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENW-40, 35, 18, 25)];
        [self.helpImage setImage:[UIImage imageNamed:@"redMoney"]];
        [self addSubview:self.helpImage];
        self.helpImage.hidden = YES;
        
        self.line = [[UILabel alloc]initWithFrame:CGRectMake(10, 69, SCREENW, 0.3)];
        self.line.backgroundColor =[UIColor grayColor];
        self.line.alpha = 0.3;
        [self addSubview:self.line];
        
        
        self.addBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-70, 20, 50, 30)];
        [self.addBtn setTitle:@"添加" forState:UIControlStateNormal];
        [self.addBtn setTitleColor:bgmmainColor forState:UIControlStateNormal];
        self.addBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.addBtn setTitleColor:bgmsmallColor forState:UIControlStateSelected];
        [self.addBtn setTitle:@"已添加" forState:UIControlStateSelected];
        self.addBtn.backgroundColor = bgmyelloeColor;
        self.addBtn.layer.cornerRadius = 12;
        self.addBtn.layer.masksToBounds = YES;
        self.addBtn.hidden = YES;
        [self addSubview:self.addBtn];
        
        [self.addBtn addTarget:self action:@selector(clickAdd:) forControlEvents:UIControlEventTouchUpInside];

    }
    return self;
}





-(void)messageIno:(NSDictionary *)dic{
    self.addBtn.hidden = YES;

    self.helpImage.hidden = YES;
    
    NSString *pushIcons = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pushIcons"]];
    NSArray *array = [pushIcons componentsSeparatedByString:@","];

    NSString *pushContent = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pushContent"]];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:pushContent];
    
    NSString *status = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spareTwo"]];  //1未读 2已读
    if ([status isEqualToString:@"1"]) {
        self.readStatus.hidden = NO;
    }else{
        self.readStatus.hidden = YES;
    }
    
    
    for (NSString *str in array) {
        NSRange range = [pushContent rangeOfString:str];
         [string addAttribute:NSForegroundColorAttributeName value:bgmyelloeColor range:range];
    }
    self.messageLabel.attributedText = string;
    

    self.nameLabel.text =[NSString stringWithFormat:@"%@",[dic objectForKey:@"pushTitle"]];
    self.timeLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"creatTime"]];
    NSString *type = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spareThree"]];
    if ([type isEqualToString:@"notice_of_action"]) { //受理通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_1"]];
    }else if ([type isEqualToString:@"comment"]){  //评论通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_2"]];
    }else if ([type isEqualToString:@"join_group"]){  //邀请加入讨论组
        [self.headImage setImage:[UIImage imageNamed:@"noti_3"]];
    }else if ([type isEqualToString:@"help_finish"]){  //帮忙完成
        [self.headImage setImage:[UIImage imageNamed:@"noti_4"]];
    }else if ([type isEqualToString:@"help"]){  //帮转通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_5"]];
    }else if ([type isEqualToString:@"system_update"]){  //系统更新通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_6"]];
    }else if ([type isEqualToString:@"term_reminding"]){  //期限提醒
        [self.headImage setImage:[UIImage imageNamed:@"noti_7"]];
    }else if ([type isEqualToString:@"spectral_value"]){  //获得靠谱值通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_7"]];
    }else if ([type isEqualToString:@"violation_reminding"]){  //违规提醒
        [self.headImage setImage:[UIImage imageNamed:@"noti_8"]];
    }else if ([type isEqualToString:@"red_pacakge_return"]){  //红包余额退回通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_9"]];
    }else if ([type isEqualToString:@"report_feedback"]){  //举报反馈通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_8"]];
    }else if ([type isEqualToString:@"new_fridens"]){  //新的朋友通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_9"]];
    }else if ([type isEqualToString:@"getRed_package"]){  //红包通知 帮转
        [self.headImage setImage:[UIImage imageNamed:@"noti_9"]];
          self.helpImage.hidden = NO;
        if ([status isEqualToString:@"1"]) {
            self.readStatus.hidden = NO;
        }else{
            [self.helpImage setImage:[UIImage imageNamed:@"norMoney"]];

        }
      
    }else if ([type isEqualToString:@"thank_you"]){  //答谢金通知
        [self.headImage setImage:[UIImage imageNamed:@"noti_9"]];
        self.helpImage.hidden = NO;
        if ([status isEqualToString:@"1"]) {
            self.readStatus.hidden = NO;
        }else{
            [self.helpImage setImage:[UIImage imageNamed:@"norMoney"]];
            
        }
    }
}

- (void)setTeamMessageInfo:(NIMMessage *)message{
    self.addBtn.hidden = YES;

    [self.headImage setImage:[UIImage imageNamed:@"groupHead"]];
    NSString *otherid  = message.session.sessionId;
    NIMTeam *team = [[[NIMSDK sharedSDK]teamManager] teamById:otherid];
    self.nameLabel.text = team.teamName;
    self.messageLabel.text = message.text;
    self.timeLabel.text = [self timestampSwitchTime:message.timestamp andFormatter:@"YYYY-MM-dd hh:mm:ss"];
    

//
//    if (message.isOutgoingMsg) {
//        if (message.isRemoteRead) {
//            self.readStatus.hidden = YES;
//        }else{
//            self.readStatus.hidden = NO;
//        }
//    }else{
//        self.readStatus.hidden = YES;
//    }

}
- (void)contentSession:(NIMMessage *)message{
    self.addBtn.hidden = YES;
    NSString *otherid  = message.session.sessionId;
    NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:otherid];
    self.nameLabel.text =user.userInfo.nickName;
    self.messageLabel.text = message.text;
//    if (message.isOutgoingMsg) {
//        if (message.isRemoteRead) {
//            self.readStatus.hidden = YES;
//        }else{
//            self.readStatus.hidden = NO;
//        }
//    }else{
//        self.readStatus.hidden = YES;
//    }
    
    
   
    self.timeLabel.text = [self timestampSwitchTime:message.timestamp andFormatter:@"YYYY-MM-dd hh:mm:ss"];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:user.userInfo.avatarUrl] placeholderImage:[UIImage imageNamed:@"headImage"]];
}
- (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
    
}

- (void)userDetailnfo:(NSDictionary *)dic{
    self.addBtn.hidden = YES;

    self.line.frame = CGRectMake(60, 69, SCREENW-60, 0.3);
    self.headImage.frame = CGRectMake(10, 10, 45, 45);
    self.headImage.layer.cornerRadius = 45/2;
    self.messageLabel.textColor = bgmsmallColor;
    self.timeLabel.textColor = bgmmainColor;
    self.timeLabel.font = [UIFont systemFontOfSize:18];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
  
    
    //图标
    NSString *type = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spareOne"]];
    if ([type isEqualToString:@"RED_PACKAGE_ICONS"]) {
        [self.headImage setImage:[UIImage imageNamed:@"moneyDetail_1"]];
    }else if ([type isEqualToString:@"MONEY_REWARD_ICONS"]){
        [self.headImage setImage:[UIImage imageNamed:@"moneyDetail_2"]];
    }else if ([type isEqualToString:@"CASH_WITHDRAWAL_ICONS"]){
        [self.headImage setImage:[UIImage imageNamed:@"moneyDetail_3"]];
    }else{
        
        NSString *image = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spareOne"]];
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"headImage"]];
    }
    
    self.nameLabel.text =  [NSString stringWithFormat:@"%@",[dic objectForKey:@"billDetail"]];
    self.messageLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"billTime"]];

    //billType 1- 2+
    NSString *billType = [NSString stringWithFormat:@"%@",[dic objectForKey:@"billType"]];
    self.timeLabel.text = [NSString stringWithFormat:@"%@%@",[billType isEqualToString:@"1"]?@"-":@"+" ,[dic objectForKey:@"billMoney"]];;
}
- (void)newfriendAdd:(NSDictionary *)newAdd{
    self.addBtn.hidden = NO;
    self.nameLabel.text =[NSString stringWithFormat:@"%@",[newAdd objectForKey:@"uname"]];
    self.timeLabel.text = @"";
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[newAdd objectForKey:@"headImage"]]] placeholderImage:[UIImage imageNamed:@"headImage"]];
    
    NSString *contentstr = [NSString stringWithFormat:@"%@",[newAdd objectForKey:@"msg"]];
     NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:contentstr];
    
    NSString *yellowstr = [NSString stringWithFormat:@"%@",[newAdd objectForKey:@"lable"]];
    NSRange range = [contentstr rangeOfString:yellowstr];
    [string addAttribute:NSForegroundColorAttributeName value:bgmyelloeColor range:range];
    self.messageLabel.attributedText = string;
    
    //state 1 没有添加 2 已经添加
    NSString *state = [NSString stringWithFormat:@"%@",[newAdd objectForKey:@"state"]];
    if ([state isEqualToString:@"1"]) {
        self.addBtn.selected = NO;
        self.addBtn.backgroundColor = bgmyelloeColor;
    }else{
        self.addBtn.selected = YES;
        self.addBtn.backgroundColor = [UIColor clearColor];
    }
}
- (void)clickAdd:(UIButton *)btn{
    [self.msgDelegate clickAddFriend:(int)btn.tag];
}
@end
